<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :span="24">
            <a-breadcrumb>
              <a-breadcrumb-item href="">
                <a-icon type="home" />
                <span>首页</span>
              </a-breadcrumb-item>
              <a-breadcrumb-item href="">
                <a-icon type="user" />
                <span>质检单管理</span>
              </a-breadcrumb-item>
              <a-breadcrumb-item href="">
                <span>
                <template v-if="this.id !== ''"> <a-icon type="edit" />质检单编辑</template>
                  <template v-else> <a-icon type="folder-add" />质检单添加</template>
                </span>
              </a-breadcrumb-item>
            </a-breadcrumb>
          </a-col>
          <a-col :span="24">&nbsp;</a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <div class="table-page-search-wrapper">
      <a-form-model :model="formData">
        <a-row :gutter="24">
          <a-col :span="16">
            <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-button-group>
                <a-button type="primary" @click="goBack"> <a-icon type="left" />返回 </a-button>
              </a-button-group>
            </a-form-item>
          </a-col>
          <a-col :span="8" :style="{ textAlign: 'right' }">
            <a-form-item>
              <a-button-group>
                <a-button type="primary" @click="toAudit">
                 审批 <a-icon type="right" />
                </a-button>
              </a-button-group>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :span="8">
            <a-form-model-item style="display: none;width:0px;" :labelCol="labelCol" :wrapperCol="wrapperCol"  label="id">
              <a-input v-model="formData.id" />
            </a-form-model-item>
            <a-form-item label="质检单编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.qualityCode" :disabled="disabledOne" placeholder="请输入质检单编号"></a-input>
            </a-form-item>
            <a-form-model-item label="质检单名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.qualityName" :disabled="disabledOne" placeholder="请输入质检单名称"></a-input>
            </a-form-model-item>
            <a-form-model-item label="订单编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.orderCode" :disabled="disabledOne" placeholder="请输入订单编号"></a-input>
            </a-form-model-item>
            <a-form-model-item label="订单名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.orderName" :disabled="disabledOne" placeholder="请输入订单名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="供货商编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.supplierCode" readOnly :disabled="disabledOne" placeholder="请输入供货商编号" ></a-input>
            </a-form-model-item>
            <a-form-model-item label="供货商名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.supplierName" :disabled="disabledOne" placeholder="请输入供货商名称"></a-input>
            </a-form-model-item>
            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.createBy" :disabled="disabledOne" placeholder="请输入创建人"></a-input>
            </a-form-model-item>
            <a-form-model-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择创建日期" v-model="formData.createTime" showTime
                      :dateFormat="dateFormat" :disabled="disabledOne" style="width: 100%"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.status" :disabled="disabledOne" placeholder="请输入状态"></a-input>
            </a-form-model-item>
            <a-form-model-item label="总数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.totalNum" :disabled="disabledOne" placeholder="请输入总数量"></a-input>
            </a-form-model-item>
            <a-form-model-item label="总金额" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-model="formData.totalPrice" :disabled="disabledOne" placeholder="请输入总金额"></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </div>


    <!-- 操作按钮区域 -->
    <div class="table-operator">
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table ref="table" bordered :data-source="dataSource" :columns="columns">
        <template slot="qualifiedNum" slot-scope="text, record">
          <editable-cell :text="text" @change="onCellChange(record, 'qualifiedNum', $event)" />
        </template>
      </a-table>
    </div>

  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@lone/lone-vue/src/utils/mixin'
  import { JEditableTableMixin } from '@lone/lone-vue/src/mixins/JEditableTableMixin'
  import JDate from '@lone/lone-vue/src/components/jeecg/JDate'
  import { postAction, putAction, deleteAction, getAction } from '@lone/lone-vue/src/api/manage'
  import EditableCell from '../base/form/EditableCell'
  import Vue from 'vue'

  export default {
    name: 'MmsWzPurchaseQualityDetailsList',
    mixins:[JEditableTableMixin, mixinDevice],
    components: {
      JDate,
      EditableCell
    },
    props:{
      //判断是编辑还是新增
      status:{
        type:String,
        required:false
      },
      //判断是编辑Id
      id:{
        type:String,
        required:false
      },
    },
    watch:{
      status: {
        handler(newVal, oldVal) {
          this.loadFormData()
        },
        immediate: true
      }
    },
    data () {
      return {
        description: 'mms_wz_purchase_quality_details管理页面',
        top: 10,
        dateFormat: 'YYYY-MM-DD HH:mm:ss',
        // 表头
        disabledAll: false,
        disabledOne: false,
        editingKey: '',
        model: {},
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'物资编码',
            align:"center",
            dataIndex: 'materialCode'
          },
          {
            title:'物资名称',
            align:"center",
            dataIndex: 'materialName'
          },
          {
            title:'规格',
            align:"center",
            dataIndex: 'specs'
          },
          {
            title:'型号',
            align:"center",
            dataIndex: 'model'
          },
          {
            title:'计量单位',
            align:"center",
            dataIndex: 'unit'
          },
          {
            title:'到货数量',
            align:"center",
            dataIndex: 'currentArrivalNum'
          },
          {
            title:'合格数量',
            align:"center",
            dataIndex: 'qualifiedNum',
            scopedSlots: { customRender: 'qualifiedNum' },
          },
          {
            title:'单价',
            align:"center",
            dataIndex: 'price'
          },
          {
            title:'总金额',
            align:"center",
            dataIndex: 'totalPrice'
          }
        ],
        formData:{},
        dataSource: [],
        url: {
          list: "/cg/mmsWzPurchaseQualityDetails/list",
          delete: "/cg/mmsWzPurchaseQualityDetails/delete",
          deleteBatch: "/cg/mmsWzPurchaseQualityDetails/deleteBatch",
          exportXlsUrl: "/cg/mmsWzPurchaseQualityDetails/exportXls",
          importExcelUrl: "cg/mmsWzPurchaseQualityDetails/importExcel",
        },
        dictOptions:{},
      }
    },
    created() {
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      loadFormData() {
        let that = this;
        this.$set(this.formData, 'createTime', Vue.prototype.$formatDate());
        this.$set(this.formData, 'status', '新建');
        this.dataSource = [];
        getAction('/base/mmsMaterialCode/getUserInfo', {}).then((res) => {
          if (res.success) {
            let result = res.result;
            if(result){
              this.$set(this.formData, 'createBy', result['realname']);
            }
          }
        })
        if(this.id !== '@ADD@'){
          let paramsObject = {id: this.id};
          getAction('/cg/mmsWzPurchaseQuality/queryById', paramsObject).then((res) => {
            if (res.success) {
              let result = res.result;
              if (result) {
                for(let p in result) {
                  if(p === 'details'){
                    const records = result[p];
                    for(let r in records){
                      let newData = records[r];
                      this.dataSource.push(newData);
                    }
                  }else{
                    this.$set(this.formData, p, result[p]);
                  }
                }

                /* if(status === 'U'){
                   that.disabledOne = true;
                 }*/
                /*else if(status === 'C'){
                  this.disabledAll = true;
                }*/
              }
            }
          });
        }
        that.disabledOne = true;
      },
      onCellChange(record, dataIndex, value) {
        let materialCode = record.materialCode;
        const dataSource = [...this.dataSource];
        const target = dataSource.find(item => item.materialCode === materialCode);
        if (target) {
          target[dataIndex] = value;
          dataSource[dataSource.indexOf(target)] = target;
          this.dataSource = dataSource;
        }
      },
      toAudit(){
        let that = this, id = that.formData.id;
        postAction('/cg/mmsWzPurchaseQuality/audit', {id: id, details: this.dataSource}).then((res) => {
          if (res.success) {
            that.$message.success(res.message);
            let result = res.result;
            if (result) {
              that.$emit("prevStep");
              that.$emit("nextStep",{id:id,status: ''})
            }
          }else {
            that.$message.warning(res.message);
          }
        })
      },
      goBack(){
        this.$emit('prevStep')
      },
      initDictConfig(){
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>